//
//  TRTabBarViewController.m
//  WeiMiLan
//
//  Created by Mac on 14-7-16.
//  Copyright (c) 2014年 Mac. All rights reserved.
//

#import "TRTabBarViewController.h"
#import "MenuView.h"

@interface TRTabBarViewController ()
@property (strong, nonatomic) TRUploadEditerViewController *uploadVC;
@end

@implementation TRTabBarViewController

- (TRUploadEditerViewController *)uploadVC {
    if (_uploadVC == nil) {
        _uploadVC = [TRUploadEditerViewController new];
    }
    return _uploadVC;
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    NSMutableArray *vcs = [[NSMutableArray alloc] init];
    [vcs addObject:[[UINavigationController alloc] initWithRootViewController:[TRFindViewController new]]];
    [vcs addObject:[[UINavigationController alloc] initWithRootViewController:[TRTypeViewController new]]];
    [vcs addObject:[[UINavigationController alloc] initWithRootViewController:[TRUploadEditerViewController new]]];
    [vcs addObject:[[UINavigationController alloc] initWithRootViewController:[TRMyOrderViewController new]]];
    [vcs addObject:[[UINavigationController alloc] initWithRootViewController:[TRMyhomeViewController new]]];
    self.viewControllers = vcs;
    [self initTabbarView];
    
    
    if ([NSStandardUserDefaults objectForKey:@"USER_NAME"]) {
        ApplicationDelegate.userName=[NSStandardUserDefaults objectForKey:@"USER_NAME"];
        ApplicationDelegate.password=[NSStandardUserDefaults objectForKey:@"USER_PASSWORD"];
        ApplicationDelegate.currentUserId=[NSStandardUserDefaults objectForKey:@"USER_ID"];
        ApplicationDelegate.uploadUrl=[NSURL URLWithString:[NSStandardUserDefaults objectForKey:@"USER_URL"]];
        NSDictionary *userInfo = [[QLUserManager shareInstance] getLastUserInfoFromNative];
        int uid = [userInfo ql_intForKey:@"ID"];
        [[QLFollowManager shareInstance] loadAllWithUserId:uid callback:nil];
        [[QLFavoriteManager shareInstance] loadAllWithUserId:uid callback:nil];
    }
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    [self.customTabBarView updateView];
}

-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
}

-(void)initTabbarView{
    
    NSArray *nibObjects = [[NSBundle mainBundle] loadNibNamed:@"CustomTabBarView" owner:self options:nil];
    self.customTabBarView = [nibObjects objectAtIndex:0];
    self.customTabBarView.delegate = self;
    [self.customTabBarView initView];
    
    self.customTabBarView.height = 50;
    self.customTabBarView.width = [UIScreen mainScreen].bounds.size.width;
    
    CGRect r = self.customTabBarView.frame;
    r.origin.y = self.view.frame.size.height - self.customTabBarView.frame.size.height ;
    self.customTabBarView.frame = r;
   
    [self.view addSubview:self.customTabBarView];
}

#pragma mark CustomTabBarViewDelegate

-(void)buttonWasSelected:(NSInteger)index {

    UINavigationController * navi = self.viewControllers[index];
     if (index == 2) {
      //   [self addChildViewController:self.uploadVC];
        // [self presentViewController:vc animated:YES completion:nil];
         [self.uploadVC show];
         return;
    }
    
    [navi popToRootViewControllerAnimated:YES];
    [self setSelectedIndex:index];
}

- (void)tapClicked:(UITapGestureRecognizer *)tap
{
    UIView *v=tap.view;
    [self.mydelegate menuViewDidSelecte:v.tag];
}

- (void)clearCacheAction
{
    [[SDImageCache sharedImageCache] clearDisk];
    [[SDImageCache sharedImageCache] clearMemory];
}

@end
